package com.waghost.bridalgown.utils;

import com.alibaba.fastjson.JSONObject;
import com.waghost.bridalgown.entity.UserInfo;
import com.waghost.bridalgown.exception.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class UserUtils{

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public UserInfo getUser(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        if (null == authorization){
            throw new UserException("Authorization header is required");
        }
        String userString = stringRedisTemplate.opsForValue().get(authorization);
        if (null == userString){
            throw new UserException("Authorization header is required");
        }
        UserInfo userInfo = JSONObject.parseObject(userString, UserInfo.class);
        return userInfo;
    }
}
